import { request } from '@/utils/request';
import type { PublishTalkParams, PaginationParams, CommentParams } from '@/models/talk';

// 说说相关API接口

/**
 * 发布说说
 */
export async function publishTalk(params: PublishTalkParams | FormData) {
  return request('/talk/publish', {
    method: 'POST',
    data: params,
  });
}

/**
 * 获取说说列表
 */
export async function getTalkList(params: PaginationParams) {
  return request('/talk/list', {
    method: 'GET',
    params,
  });
}

/**
 * 根据用户ID获取说说列表
 */
export async function getTalkListByUserId(userId: string | number, params: PaginationParams) {
  return request(`/talk/list/${userId}`, {
    method: 'GET',
    params,
  });
}

/**
 * 删除说说
 */
export async function deleteTalk(talkId: number, userId: string | number) {
  return request(`/talk/${talkId}`, {
    method: 'DELETE',
    params: { userId },
  });
}

/**
 * 切换点赞状态
 */
export async function toggleLike(talkId: number, userId: string | number) {
  return request('/talk/like/toggle', {
    method: 'POST',
    params: { talkId, userId },
  });
}

/**
 * 检查是否已点赞
 */
export async function checkLikeStatus(talkId: number, userId: string | number) {
  return request('/talk/like/check', {
    method: 'GET',
    params: { talkId, userId },
  });
}

/**
 * 获取用户点赞的说说ID列表
 */
export async function getLikedTalkIds(userId: string | number) {
  return request('/talk/like/list', {
    method: 'GET',
    params: { userId },
  });
}

/**
 * 添加评论（统一接口，支持登录用户和匿名用户）
 */
export async function addComment(params: {
  talkId: number;
  content: string;
  userId: string | number;
  parentId?: number;
  replyToUserId?: string | number;
  // 匿名用户专用字段（15位userId时需要提供）
  name?: string;
  email?: string;
  link?: string;
}) {
  // 过滤掉undefined值，避免传递"undefined"字符串
  const cleanParams: any = {
    talkId: params.talkId,
    content: params.content,
    userId: params.userId,
  };

  // 只添加有值的可选参数
  if (params.parentId !== undefined && params.parentId !== null) {
    cleanParams.parentId = params.parentId;
  }
  if (params.replyToUserId !== undefined && params.replyToUserId !== null) {
    cleanParams.replyToUserId = params.replyToUserId;
  }
  if (params.name) {
    cleanParams.name = params.name;
  }
  if (params.email) {
    cleanParams.email = params.email;
  }
  if (params.link) {
    cleanParams.link = params.link;
  }

  return request('/talk/comment/add', {
    method: 'POST',
    params: cleanParams,
  });
}

/**
 * 获取评论树形结构
 */
export async function getCommentTree(talkId: number) {
  return request(`/talk/comment/tree/${talkId}`, {
    method: 'GET',
  });
}

/**
 * 获取评论平铺列表
 */
export async function getCommentList(talkId: number) {
  return request(`/talk/comment/list/${talkId}`, {
    method: 'GET',
  });
}

/**
 * 获取回复列表
 */
export async function getReplyList(parentId: number) {
  return request(`/talk/comment/replies/${parentId}`, {
    method: 'GET',
  });
}

/**
 * 删除评论
 */
export async function deleteComment(commentId: number, userId: string | number) {
  return request(`/talk/comment/${commentId}`, {
    method: 'DELETE',
    params: { userId },
  });
}

/**
 * 生成临时用户ID（15位格式：YYMMDDHHMMSS + 3位随机数）
 */
export async function generateTempUserId() {
  return request('/talk/comment/generate-temp-user-id', {
    method: 'GET',
  });
}